<?php
/**
 * Attachments Element File
 *
 * Element listing associated attachments of the view's model
 * Add, delete (detach) an Attachment
 *
 * Copyright (c) 2007-2009 David Persson
 *
 * Distributed under the terms of the MIT License.
 * Redistributions of files must retain the above copyright notice.
 *
 * PHP version 5
 * CakePHP version 1.2
 *
 * @package    media
 * @subpackage media.views.elements
 * @copyright  2007-2009 David Persson <davidpersson@gmx.de>
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link       http://github.com/davidpersson/media
 */

/**
 * New Parameter is passed, as different page may need different labels
 * $labels = array(
        file => label_for_file_input,
        alternative => label_for_alternative,
    );
 * $fields = array('alternative'=>true);
 */
$_labels = array(
    'file' => __('File', true),
    'alternative' => __('Textual replacement', true)
);
$_fields = array(
    'file' => true,
    'alternative' => true
);

if(!isset($labels)){
    $labels = $_labels;
}else{
    $labels = array_merge($_labels, $labels);
}

if(!isset($fields)){
    $fields = $_fields;
}else{
    $fields = array_merge($_fields, $fields);
}

if (!isset($previewVersion)) {
    $previewVersion = 'xxs';
}

/* Set $assocAlias and $model if you're using this element multiple times in one form */

if (!isset($assocAlias)) {
    $assocAlias = 'Attachment';
} else {
    $assocAlias = Inflector::singularize($assocAlias);
}

if (!isset($model)) {
    $model = $form->model();
}

$modelId = $form->value($form->model().'.id');

if (isset($this->data[$assocAlias][0]['basename'])) {
    array_unshift($this->data[$assocAlias],array());
}
?>
<div class="attachments element">
    <?php //printf(__('%s', true), Inflector::pluralize($assocAlias)) ?>
    <!-- New Attachment -->
    <div class="new">
    <?php
        echo $form->hidden($assocAlias . '.0.model', array('value' => $model));
        echo $form->hidden($assocAlias . '.0.group', array('value' => strtolower($assocAlias)));
        echo $form->input($assocAlias . '.0.file', array(
            'label' => $labels['file'],
            'type'  => 'file',
            'class' => 'input file',
            'error' => array(
                'error'      => __('An error occured while transferring the file.', true),
                'resource'   => __('The file is invalid.', true),
                'access'     => __('The file cannot be processed.', true),
                'location'   => __('The file cannot be transferred from or to location.', true),
                'permission' => __('Executable files cannot be uploaded.', true),
                'size'       => __('The file is too large.', true),
                'pixels'     => __('The file is too large.', true),
                'extension'  => __('The file has the wrong extension.', true),
                'mimeType'   => __('The file has the wrong MIME type.', true),
        )));
        if($fields['alternative']){
            echo $form->input($assocAlias . '.0.alternative', array(
                'label' => $labels['alternative'],
                'class' => 'input text',
                'value' => '',
                'error' => __('A textual replacement must be provided.', true)
            ));
        }
    ?>
    </div>
    <!-- Existing Attachments -->
    <div class="existing">
    <?php if (isset($this->data[$assocAlias]) && count($this->data[$assocAlias])): ?>
        <h3><?php __('Existing Attachments'); ?></h3>
        <?php for($i = 1; $i < count($this->data[$assocAlias]); $i++): ?>
        <div class='attachment'>
        <?php
            $item = $this->data[$assocAlias][$i];

            echo $form->hidden($assocAlias . '.' . $i . '.id', array('value' => $item['id']));
            echo $form->hidden($assocAlias . '.' . $i . '.model', array('value' => $model));
            echo $form->hidden($assocAlias . '.' . $i . '.group', array('value' => $item['group']));
            echo $form->hidden($assocAlias . '.' . $i . '.dirname', array('value' => $item['dirname']));
            echo $form->hidden($assocAlias . '.' . $i . '.basename', array('value' => $item['basename']));
            echo $form->hidden($assocAlias . '.' . $i . '.alternative', array('value' => $item['alternative']));

            if ($file = $medium->file($item)) {

                echo $medium->embed($medium->file(
                    $previewVersion . '/', $item),
                    array('restrict' => array('image')
                ));

                 $Medium = Medium::factory($file);
                $size = $medium->size($file);

                if (isset($number)) {
                    $size = $number->toReadableSize($size);
                } else {
                    $size .= ' Bytes';
                }

                //debug($Medium);

                printf('<span>%s&nbsp;(%s)</span>',
                        $html->link(
                            empty($item['alternative']) ? $item['basename'] : $item['alternative'],
                            array('controller'=>'posts','action'=>'download', $item['id'])),
                        $size);
            }
            echo '<br/>';
            echo $form->input($assocAlias . '.' . $i . '.delete', array(
                'label' => __('Release', true),
                'type'  => 'checkbox',
                'class' => 'checkbox',
                'value' => 0
            ));
        ?>
        </div>
        <?php endfor ?>
    <?php endif ?>
    </div>
</div>
